package it.niedermann.android.markdown

import android.content.Context
import android.graphics.Bitmap
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ImageSpan
import androidx.annotation.RestrictTo
import io.noties.markwon.base.R

//import com.nextcloud.android.sso.model.SingleSignOnAccount;
@RestrictTo(RestrictTo.Scope.LIBRARY)
object MentionUtil {
    private fun replaceAtMentionsWithImagePlaceholderAndDisplayName(
        context: Context,
        mentions: Map<String, String>,
        text: CharSequence
    ): SpannableStringBuilder {
        val messageBuilder = SpannableStringBuilder(text)
        for (userId in mentions.keys) {
            val mentionId = "@$userId"
            val mentionDisplayName = " " + mentions[userId]
            var index = messageBuilder.toString().lastIndexOf(mentionId)
            while (index >= 0) {
                messageBuilder.setSpan(
                    MentionSpan(context, R.drawable.ic_person_grey600_24dp),
                    index,
                    index + mentionId.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                messageBuilder.insert(index + mentionId.length, mentionDisplayName)
                index = messageBuilder.toString().substring(0, index).lastIndexOf(mentionId)
            }
        }
        return messageBuilder
    }

    private class MentionSpan : ImageSpan {
         constructor(context: Context, resourceId: Int) : super(context, resourceId) {}
         constructor(context: Context, bitmap: Bitmap) : super(context, bitmap) {}
    }
}